import 'package:citiesocial/_include.dart';

/// 登录成功会返回一个‘1’
class Login extends BaseStatefulWidget {
  @override
  State<StatefulWidget> createState() => _LoginState();
}

class _LoginState extends BaseState<Login> {
  final _email = TextEditingController(), _password = TextEditingController();

  void _login() async {
    final email = _email.text;
    if (email.length == 0) {
      Toast.show(context, '邮件不能为空');
      return;
    }
    final password = _password.text;
    if (password.length == 0) {
      Toast.show(context, '密码不能为空');
      return;
    }
    LoadingDialog.show(context);
    final deviceId = await getDeviceId();
    final user =
        await HttpManager.login(email, password, deviceId, context: context);
    await LoadingDialog.dismiss(context);
    if (user != null && user.user_id != null && user.user_id.length > 0) {
      Toast.show(context, '登录成功').then((_) {
        Configs.saveUserState(user);
        Navigator.pop(context, '1');
      });
    }
  }

  void _loadUserAgreement() async {
    LoadingDialog.show(context);
    final agreement = await HttpManager.userAgreement(context);
    await LoadingDialog.dismiss(context);
    if (agreement != null &&
        agreement.link != null &&
        agreement.link.length > 0) {
      Navigator.push(
        context,
        MaterialPageRoute(
          builder: (_) => WebviewScaffold(
                appBar: AppBarUtils.normal(context, agreement.title),
                url: '${agreement.link}',
              ),
        ),
      );
    }
  }

  @override
  void dispose() {
    _email.dispose();
    _password.dispose();

    super.dispose();
  }

  @override
  Widget build(BuildContext context) => Scaffold(
        resizeToAvoidBottomPadding: false,
        appBar: AppBarUtils.normal(context, '登录'),
        backgroundColor: Colors.white,
        body: SafeArea(
          child: Container(
            color: Colors.white,
            child: Column(
              children: <Widget>[
                Expanded(
                  child: Column(
                    children: <Widget>[
                      Container(
                        decoration: ShapeDecoration(
                          shape: RoundedRectangleBorder(
                            side: BorderSide(color: Configs.main),
                            borderRadius:
                                BorderRadius.all(Radius.circular(8.0)),
                          ),
                        ),
                        padding: EdgeInsets.symmetric(horizontal: 10.0),
                        child: Column(
                          children: <Widget>[
                            TextFormField(
                              controller: _email,
                              maxLines: 1,
                              decoration: InputDecoration(
                                  hintText: '电子邮件', border: InputBorder.none),
                            ),
                            Configs.dividerLine,
                            TextFormField(
                              obscureText: true,
                              controller: _password,
                              maxLines: 1,
                              decoration: InputDecoration(
                                  hintText: '密码', border: InputBorder.none),
                            ),
                          ],
                        ),
                        margin: EdgeInsets.all(15.0),
                      ),
                      Container(
                        child: RaisedButton(
                          color: Configs.main,
                          textColor: Colors.white,
                          child: Text(
                            '登录',
                            style: TextStyle(fontSize: 18.0),
                          ),
                          onPressed: _login,
                          padding: EdgeInsets.symmetric(vertical: 10.0),
                        ),
                        width: double.infinity,
                        margin: EdgeInsets.symmetric(horizontal: 15.0),
                      ),
                    ],
                  ),
                ),
                Container(
                  child: Column(
                    children: <Widget>[
                      Container(
                        child: Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: <Widget>[
                            GestureDetector(
                              child: Text(
                                '立即注册',
                                style: TextStyle(
                                    fontSize: 16.0, color: Colors.grey),
                              ),
                              onTap: () {
                                Navigator.pushNamed(context, '/register')
                                    .then((result) {
                                  if (result == '1') {
                                    Navigator.maybePop(context, '1');
                                  }
                                });
                              },
                            ),
                            Container(
                              decoration: BoxDecoration(
                                  shape: BoxShape.rectangle,
                                  color: Colors.grey),
                              height: 10.0,
                              width: Configs.lineHeight,
                              margin: EdgeInsets.symmetric(horizontal: 10.0),
                            ),
                            GestureDetector(
                              child: Text(
                                '忘记密码',
                                style: TextStyle(
                                    fontSize: 16.0, color: Colors.grey),
                              ),
                              onTap: () {
                                Navigator.pushNamed(context, '/forgot');
                              },
                            ),
                          ],
                        ),
                        margin: EdgeInsets.only(bottom: 10.0),
                      ),
                      GestureDetector(
                        child: Row(
                          mainAxisSize: MainAxisSize.min,
                          children: <Widget>[
                            Text(
                              '登录等同您同意',
                              style:
                                  TextStyle(fontSize: 12.0, color: Colors.grey),
                            ),
                            Text(
                              '<服务条款>',
                              style: TextStyle(
                                  fontSize: 12.0, color: Configs.main),
                            ),
                          ],
                        ),
                        onTap: _loadUserAgreement,
                      ),
                    ],
                  ),
                  margin: EdgeInsets.only(
                    bottom: 10.0,
                  ),
                ),
              ],
            ),
          ),
        ),
      );
}
